import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**随机事件-随机任务属性枚举*/
export const enum E_RandomEventMission {
	/**关卡事件id*/
	EVENT_ID = 0,
	/**开始时间*/
	START_TIME = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**随机事件-随机任务*/
export class RandomEventMission {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_RANDOM_EVENT_MISSION_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_RANDOM_EVENT_MISSION_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: RandomEventMission[] = [];

	/**创建入口*/
	public static create(json?: string): RandomEventMission {
		const c = this.POOL.pop() || new RandomEventMission();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**关卡事件id (uint32)*/
	protected _eventId: number;
	/**开始时间 (uint16)*/
	protected _startTime: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (RandomEventMission.POOL.indexOf(this) === -1) {
			RandomEventMission.POOL.push(this);
		}
	}

	/**关卡事件id*/
	public get eventId() {
		return this._eventId;
	}


	/**关卡事件id*/
	public set eventId(v: number) {
		this._eventId = v;
	}


	/**开始时间*/
	public get startTime() {
		return this._startTime;
	}


	/**开始时间*/
	public set startTime(v: number) {
		this._startTime = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_RandomEventMission): any {
		switch (k) {
			case E_RandomEventMission.EVENT_ID: return this._eventId;
			case E_RandomEventMission.START_TIME: return this._startTime;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_RandomEventMission, v: any): void {
		switch (k) {
			case E_RandomEventMission.EVENT_ID: this._eventId = v; break;
			case E_RandomEventMission.START_TIME: this._startTime = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//关卡事件id
		this._eventId = 0;
		//开始时间
		this._startTime = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._eventId) {
			json.eventId = this._eventId;
		}
		if (this._startTime) {
			json.startTime = this._startTime;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.eventId) {
			this._eventId = json.eventId;
		}
		if (json.startTime) {
			this._startTime = json.startTime;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._eventId);
		byte.writeUint16(this._startTime);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._eventId = byte.readUint32();
		this._startTime = byte.readUint16();
		return this;
	}


	/**克隆*/
	public clone(): RandomEventMission {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = RandomEventMission.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = RandomEventMission.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//关卡事件id
			if (idx === E_RandomEventMission.EVENT_ID) {
				const oldv = this._eventId;
				this._eventId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._eventId, oldv);
				i += 2;
				continue;
			}
			//开始时间
			if (idx === E_RandomEventMission.START_TIME) {
				const oldv = this._startTime;
				this._startTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._startTime, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in RandomEventMission with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: RandomEventMission): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._eventId !== other._eventId) {
			updates.push(E_RandomEventMission.EVENT_ID, this._eventId);
		}
		if (this._startTime !== other._startTime) {
			updates.push(E_RandomEventMission.START_TIME, this._startTime);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(RandomEventMission.TOTAL_EVENT_ID, this.getV(0));
	}

}
